/******************************************************************************
 Copyright (c) 2020-2024 Qualcomm Technologies International, Ltd.
 All Rights Reserved.
 Qualcomm Technologies International, Ltd. Confidential and Proprietary.

******************************************************************************/

#include "csr_synergy.h"

#include "csr_bt_cm_main.h"
#include "csr_bt_cm_dm.h"
#include "csr_log_text_2.h"
#include "csr_bt_cm_le.h"

#ifdef CSR_BT_INSTALL_PERIODIC_ADVERTISING
void CsrBtCmDmPeriodicAdvSetParamsReqHandler(cmInstanceData_t *cmData)
{
    CmPeriodicAdvSetParamsReq *prim = cmData->recvMsgP;
    cmData->dmVar.appHandle = prim->appHandle;
    cmData->advHandle = prim->advHandle;

    dm_periodic_adv_set_params_req(prim->advHandle,
                                prim->flags,
                                prim->periodicAdvIntervalMin,
                                prim->periodicAdvIntervalMax,
                                prim->periodicAdvProperties,
                                NULL);
}

void CsrBtCmDmPeriodicAdvSetDataReqHandler(cmInstanceData_t *cmData)
{
    CmPeriodicAdvSetDataReq *prim = (CmPeriodicAdvSetDataReq *)cmData->recvMsgP;
    DM_HCI_ULP_PERIODIC_ADV_SET_DATA_REQ_T *dmPrim;

    cmData->dmVar.appHandle = prim->appHandle;
    cmData->advHandle = prim->advHandle;

    /* We are not using dm_periodic_adv_set_data_req dmlib API, as it requires us to reassemble all buffers into one
     * and pass it to the API. However in the implementation of dm_periodic_adv_set_data_req, we are segmenting it again
     * into multiple buffers before sending this to bluestack. This defies the kind of interface synergy has given
     * where smaller buffers will be allocated instead of one large buffer. This is the reason we will be sending
     * the DM prim from here.*/
    dmPrim = CsrPmemZalloc(sizeof(DM_HCI_ULP_PERIODIC_ADV_SET_DATA_REQ_T));
    dmPrim->common.op_code = DM_HCI_ULP_PERIODIC_ADV_SET_DATA_REQ;
    dmPrim->adv_handle = prim->advHandle;
    dmPrim->operation = prim->operation;
    dmPrim->adv_data_len = prim->dataLength;

    if (prim->dataLength)
    {
        CsrUint8 index;

        for (index = 0; index < CM_PERIODIC_ADV_DATA_BYTES_PTRS_MAX && prim->data[index] != NULL; index++)
        {
            dmPrim->adv_data[index] = prim->data[index];
            /* This should not get freed accidentaly.*/
            prim->data[index] = NULL;
        }
    }

    DM_PutMsg(dmPrim);
}

void CsrBtCmDmPeriodicAdvReadMaxAdvDataLenReqHandler(cmInstanceData_t *cmData)
{
    CmPeriodicAdvReadMaxAdvDataLenReq *prim = cmData->recvMsgP;
    cmData->dmVar.appHandle = prim->appHandle;
    cmData->advHandle = prim->advHandle;

    dm_periodic_adv_read_max_adv_data_len_req(prim->advHandle, NULL);
}

void CsrBtCmDmPeriodicAdvStartReqHandler(cmInstanceData_t *cmData)
{
    CmPeriodicAdvStartReq *prim = cmData->recvMsgP;
    cmData->dmVar.appHandle = prim->appHandle;
    cmData->advHandle = prim->advHandle;

    dm_periodic_adv_start_req(prim->advHandle, NULL);
}

void CsrBtCmDmPeriodicAdvStopReqHandler(cmInstanceData_t *cmData)
{
    CmPeriodicAdvStopReq *prim = cmData->recvMsgP;
    cmData->dmVar.appHandle = prim->appHandle;
    cmData->advHandle = prim->advHandle;

    dm_periodic_adv_stop_req(prim->advHandle, prim->stopAdvertising, NULL);
}

void CsrBtCmDmPeriodicAdvSetTransferReqHandler(cmInstanceData_t *cmData)
{
    CmPeriodicAdvSetTransferReq *prim = cmData->recvMsgP;
    cmData->dmVar.appHandle = prim->phandle;
    cmData->advHandle = prim->advHandle;

    dm_periodic_adv_set_transfer_req(CSR_BT_CM_IFACEQUEUE,
                                    &prim->addrt,
                                    prim->serviceData,
                                    prim->advHandle,
                                    NULL);
}

void CsrBtCmDmPeriodicAdvEnableReqHandler(cmInstanceData_t *cmData)
{
    CmPeriodicAdvEnableReq *prim = cmData->recvMsgP;
    cmData->dmVar.appHandle = prim->phandle;
    cmData->advHandle = prim->adv_handle;

    dm_periodic_adv_enable_req(prim->adv_handle, prim->flags, prim->enable, NULL);
}
#endif /* End of CSR_BT_INSTALL_PERIODIC_ADVERTISING */
